package com.javaxiaobear.module.system.mapper;

import static com.javaxiaobear.module.system.domain.table.SysRoleDeptTableDef.SYS_ROLE_DEPT;

import com.javaxiaobear.module.system.domain.SysRoleDept;
import com.mybatisflex.core.BaseMapper;
import com.mybatisflex.core.query.QueryChain;
import com.mybatisflex.core.query.QueryWrapper;
import java.util.Arrays;
import java.util.List;

/**
 * 角色与部门关联表 数据层
 *
 * @author javaxiaobear
 */
public interface SysRoleDeptMapper extends BaseMapper<SysRoleDept> {
  /** 通过角色ID删除角色和部门关联 */
  default int deleteRoleDeptByRoleId(Long roleId) {
    QueryWrapper queryWrapper = QueryWrapper.create().where(SYS_ROLE_DEPT.ROLE_ID.eq(roleId));
    return deleteByQuery(queryWrapper);
  }

  /** 批量删除角色部门关联信息 */
  default int deleteRoleDept(Long[] ids) {
    if (ids == null || ids.length == 0) {
      return 0;
    }
    QueryWrapper queryWrapper =
        QueryWrapper.create().where(SYS_ROLE_DEPT.ROLE_ID.in(Arrays.asList(ids)));
    return deleteByQuery(queryWrapper);
  }

  /** 查询部门使用数量 */
  default int selectCountRoleDeptByDeptId(Long deptId) {
    return (int)
        QueryChain.of((BaseMapper<SysRoleDept>) this)
            .where(SYS_ROLE_DEPT.DEPT_ID.eq(deptId))
            .count();
  }

  /** 批量新增角色部门信息 */
  default int batchRoleDept(List<SysRoleDept> roleDeptList) {
    if (roleDeptList == null || roleDeptList.isEmpty()) {
      return 0;
    }
    return insertBatch(roleDeptList);
  }
}
